package com.aizhixin.test.errolAlmost.dao;

import java.security.Timestamp;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.xml.crypto.Data;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.aizhixin.test.errolAlmost.dto.SteamDTO;
import com.aizhixin.test.errolAlmost.dto.loanAndNowDTO;
@Component
public class TimeDifferenceDAO {
	/*
	 * SELECT q.CREATED_DATE,NOW()FROM user_imformation as q WHERE q.OPENID=2
	 */
	@Autowired
	private JdbcTemplate jdbcTemplate;
	public Date getTimeDifference(String openId) {
	
		String sql="SELECT q.CREATED_DATE FROM user_imformation as q WHERE q.OPENID=?";
		List<loanAndNowDTO> list = jdbcTemplate.query(sql,
				new Object[] {openId},
				new RowMapper<loanAndNowDTO>() {
			public loanAndNowDTO mapRow(ResultSet rs,
					int rowNum)throws SQLException{
				loanAndNowDTO test=new loanAndNowDTO();
				test.setUserfirstLoanTime(rs.getDate("CREATED_DATE"));
				
				return test;
			
			}
			
				});
		System.out.println(list.get(0).getUserfirstLoanTime());
		Date aDate=list.get(0).getUserfirstLoanTime();
		return aDate;
		
		
	}
	public Date getTimeDifference2() {
		
		String sql="SELECT now() ";
		Date aTimestamp=jdbcTemplate.queryForObject(sql, Date.class);
		return aTimestamp;
		
	}
	public loanAndNowDTO getTime(String openId) {
		loanAndNowDTO loanAndNowDTO =new loanAndNowDTO();
		loanAndNowDTO.setNowTime(getTimeDifference2());
		loanAndNowDTO.setUserfirstLoanTime(getTimeDifference(openId));
		return loanAndNowDTO;
	}
			
}
